<?php
/*******************************
MENUS SUPPORT
 ********************************/
        add_theme_support('nav-menus');
        function register_my_menus() {
            register_nav_menus(
                array(
                    'main-menu' => __( 'Menu Chinh' )
                )
            );
        }
        add_action( 'init', 'register_my_menus' );


    /* CallBack functions for menus in case of earlier than 3.0 Wordpress version or if no menu is set yet*/

    function primarymenu(){ ?>
    <div id="mainmenu" class="ddsmoothmenu">
        <ul>
            <?php wp_list_pages('title_li='); ?>
            <?php wp_list_categories('hide_empty=1&exclude=1&title_li='); ?>
        </ul>
    </div>
    <?php }
    /* Highlight current menu item*/
    function wpse19375_nav_class( $classes, $item ){
        if( !in_array( 'current-menu-item', $classes ))
            return $classes;
        return $classes;
    }

    add_filter( 'nav_menu_css_class', 'wpse19375_nav_class', 10, 2 );
    /* END - Highlight current menu item*/


//BEGIN CUSTOM BY LAK


//ADD META-BOX PLUGIN TO THEME
define( 'RWMB_URL', trailingslashit( get_stylesheet_directory_uri() . '/inc/meta-box' ) );
define( 'RWMB_DIR', trailingslashit( STYLESHEETPATH . '/inc/meta-box' ) );
require_once RWMB_DIR . 'meta-box.php';
include 'inc/define-metabox.php';
//END ADD META-BOX PLUGIN TO THEME

require(TEMPLATEPATH . '/inc/define_taxonomy.php' );
//END CUSTOM BY LAK



//CUSTOM BY PHUC
require(TEMPLATEPATH . '/inc/custom_post.php' );

/* BREADCRUMB - custom by JKey*/
/*
function the_breadcrumb() {

    $delimiter = '&raquo;';
    $name = get_bloginfo('name'); //text for the 'Home' link
    $currentBefore = '<span class="current">';
    $currentAfter = '</span>';

    if ( !is_home() && !is_front_page() || is_paged() ) {

        echo '<div id="jk_breadcrumbs">';

        global $post;
        $home = get_bloginfo('url');
        echo '<a href="' . $home . '">' . $name . '</a> ' . $delimiter . ' ';

        if ( is_category() ) {
            global $wp_query;
            $cat_obj = $wp_query->get_queried_object();
            $thisCat = $cat_obj->term_id;
            $thisCat = get_category($thisCat);
            $parentCat = get_category($thisCat->parent);
            if ($thisCat->parent != 0) echo(get_category_parents($parentCat, TRUE, ' ' . $delimiter . ' '));
            echo $currentBefore;
            single_cat_title();
            echo $currentAfter;

        } elseif ( is_day() ) {
            echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
            echo '<a href="' . get_month_link(get_the_time('Y'),get_the_time('m')) . '">' . get_the_time('F') . '</a> ' . $delimiter . ' ';
            echo $currentBefore . get_the_time('d') . $currentAfter;

        } elseif ( is_month() ) {
            echo '<a href="' . get_year_link(get_the_time('Y')) . '">' . get_the_time('Y') . '</a> ' . $delimiter . ' ';
            echo $currentBefore . get_the_time('F') . $currentAfter;

        } elseif ( is_year() ) {
            echo $currentBefore . get_the_time('Y') . $currentAfter;

        } elseif ( is_single()) {
            if(get_post_type() == 'san-pham')
            {
                $terms = get_the_terms($id,'nhom-san-pham');
                $arr = array();
                $str = '';
                $str .= '<a href="'.get_bloginfo('url').'/san-pham">Sản phẩm</a> &raquo; ';
                foreach($terms as $term1)
                {
                    if($term1->parent == 0)
                    {
                        $str .= '<a href="'.get_term_link($term1->name, 'nhom-san-pham').'" title="'.$term1->name.'">'.$term1->name.'</a> &raquo; ';
                        foreach($terms as $term2)
                        {
                            if($term2->parent == $term1->term_id)
                            {
                                $str .= '<a href="'.get_term_link($term2->name, 'nhom-san-pham').'" title="'.$term2->name.'">'.$term2->name.'</a> &raquo; ';
                            }
                        }
                    }
                }
                echo $str;
                the_title();
            }
            else
            {
                $cat = get_the_category(); $cat = $cat[0];
                echo get_category_parents($cat, TRUE, ' ' . $delimiter . ' ');
                echo $currentBefore;
                the_title();
                echo $currentAfter;
            }

        } elseif ( is_page() && !$post->post_parent ) {
            echo $currentBefore;
            the_title();
            echo $currentAfter;


        } elseif ( is_page() && $post->post_parent ) {
            $parent_id  = $post->post_parent;
            $breadcrumbs = array();
            while ($parent_id) {
                $page = get_page($parent_id);
                $breadcrumbs[] = '<a href="' . get_permalink($page->ID) . '">' . get_the_title($page->ID) . '</a>';
                $parent_id  = $page->post_parent;
            }
            $breadcrumbs = array_reverse($breadcrumbs);
            foreach ($breadcrumbs as $crumb) echo $crumb . ' ' . $delimiter . ' ';
            echo $currentBefore;
            the_title();
            echo $currentAfter;


        } elseif ( is_search() ) {
            echo $currentBefore . 'Kết quả tìm kiếm &#39;' . get_search_query() . '&#39;' . $currentAfter;

        } elseif ( is_tag() ) {
            echo $currentBefore . 'tagged &#39;';
            single_tag_title();
            echo '&#39;' . $currentAfter;

        } elseif ( is_author() ) {
            global $author;
            $userdata = get_userdata($author);
            echo $currentBefore . 'Articles posted by ' . $userdata->display_name . $currentAfter;

        } elseif ( is_404() ) {
            echo $currentBefore . 'Error 404' . $currentAfter;

        }  elseif (is_taxonomy('nhom-san-pham')) {
            $str = '';
            $str .= '<a href="'.get_bloginfo('url').'/san-pham">Sản phẩm</a> &raquo; ';
            if(get_queried_object()->parent == 0)
                $str .= get_queried_object()->name;
            else
            {
                $cur_term = get_the_terms('','nhom-san-pham');
                $parent_term_name = $cur_term[get_queried_object()->parent]->name;
                $str .= '<a href="'.get_term_link($parent_term_name, 'nhom-san-pham').'" title="'.$parent_term_name.'">'.$parent_term_name.'</a> &raquo; '.get_queried_object()->name;

            }
            echo $str;
        }
        if ( get_query_var('paged') ) {
            if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ' (';
            echo __('Page') . ' ' . get_query_var('paged');
            if ( is_category() || is_day() || is_month() || is_year() || is_search() || is_tag() || is_author() ) echo ')';
        }

        echo '</div>';

    }
}*/


/* END BREADCRUMB */


/*
    PHÂN TRANG
*/
//Start of Paganavi
function wp_corenavi() {
/*    global $wp_query, $wp_rewrite;
    $pages = '';
    $max = $wp_query->max_num_pages;
    if (!$current = get_query_var('paged')) $current = 1;
    $a['base'] = ($wp_rewrite->using_permalinks()) ? user_trailingslashit( trailingslashit( remove_query_arg( 's', get_pagenum_link( 1 ) ) ) . 'page/%#%/', 'paged' ) : @add_query_arg('paged','%#%');
    if( !empty($wp_query->query_vars['s']) ) $a['add_args'] = array( 's' => get_query_var( 's' ) );
    $a['total'] = $max;
    $a['current'] = $current;

    $total = 1; //1 - display the text "Page N of N", 0 - not display
    $a['mid_size'] = 5; //how many links to show on the left and right of the current
    $a['end_size'] = 1; //how many links to show in the beginning and end
    $a['prev_text'] = '&laquo; Previous'; //text of the "Previous page" link
    $a['next_text'] = 'Next &raquo;'; //text of the "Next page" link

    if ($max > 1) echo '<div class="pagenavi">';
    if ($total == 1 && $max > 1) $pages = '<span class="pages">Page ' . $current . ' of ' . $max . '</span>'."\r\n";
    echo $pages . paginate_links($a);
    if ($max > 1) echo '</div>';*/
}
// END
/*******************************
THUMBNAIL SUPPORT
 ********************************/

add_theme_support( 'post-thumbnails' );
set_post_thumbnail_size( 300, 200, true );

/* Get the thumb original image full url

function get_thumb_urlfull ($postID) {
    $image_id = get_post_thumbnail_id($post);
    $image_url = wp_get_attachment_image_src($image_id,'large');
    $image_url = $image_url[0];
    return $image_url;
}

/*******************************
EXCERPT LENGTH ADJUST
 ********************************/

/*function home_excerpt_length($length) {
    return 30;
}
add_filter('excerpt_length', 'home_excerpt_length');*/


/*******************************
WIDGETS AREAS
 ********************************/
/*
if ( function_exists('register_sidebar') )
    register_sidebar(array(
        'name' => 'sidebar',
        'before_widget' => '<div class="rightBox">',
        'after_widget' => '</div>',
        'before_title' => '<h2>',
        'after_title' => '</h2>',
    ));

register_sidebar(array(
    'name' => 'footer',
    'before_widget' => '<div class="boxFooter">',
    'after_widget' => '</div>',
    'before_title' => '<h2>',
    'after_title' => '</h2>',
));*/

/*******************************
PAGINATION
 ********************************/
/*
function emm_paginate($args = null) {
    $defaults = array(
        'page' => null, 'pages' => null,
        'range' => 3, 'gap' => 3, 'anchor' => 1,
        'before' => '<div class="emm-paginate">', 'after' => '</div>',
        'title' => __('Pages:'),
        'nextpage' => __('&raquo;'), 'previouspage' => __('&laquo'),
        'echo' => 1
    );

    $r = wp_parse_args($args, $defaults);
    extract($r, EXTR_SKIP);


    if (!$page && !$pages) {
        global $wp_query;

        $page = get_query_var('paged');
        $page = !empty($page) ? intval($page) : 1;

        $posts_per_page = intval(get_query_var('posts_per_page'));
        $pages = intval(ceil($wp_query->found_posts / $posts_per_page));
    }

    $output = "";
    if ($pages > 1) {
        $output .= "$before<span class='emm-title'>$title</span>";
        $ellipsis = "<span class='emm-gap'>...</span>";

        if ($page > 1 && !empty($previouspage)) {
            $output .= "<a href='" . get_pagenum_link($page - 1) . "' class='emm-prev'>$previouspage</a>";
        }

        $min_links = $range * 2 + 1;
        $block_min = min($page - $range, $pages - $min_links);
        $block_high = max($page + $range, $min_links);
        $left_gap = (($block_min - $anchor - $gap) > 0) ? true : false;
        $right_gap = (($block_high + $anchor + $gap) < $pages) ? true : false;

        if ($left_gap && !$right_gap) {
            $output .= sprintf('%s%s%s',
                emm_paginate_loop(1, $anchor),
                $ellipsis,
                emm_paginate_loop($block_min, $pages, $page)
            );
        }
        else if ($left_gap && $right_gap) {
            $output .= sprintf('%s%s%s%s%s',
                emm_paginate_loop(1, $anchor),
                $ellipsis,
                emm_paginate_loop($block_min, $block_high, $page),
                $ellipsis,
                emm_paginate_loop(($pages - $anchor + 1), $pages)
            );
        }
        else if ($right_gap && !$left_gap) {
            $output .= sprintf('%s%s%s',
                emm_paginate_loop(1, $block_high, $page),
                $ellipsis,
                emm_paginate_loop(($pages - $anchor + 1), $pages)
            );
        }
        else {
            $output .= emm_paginate_loop(1, $pages, $page);
        }

        if ($page < $pages && !empty($nextpage)) {
            $output .= "<a href='" . get_pagenum_link($page + 1) . "' class='emm-next'>$nextpage</a>";
        }

        $output .= $after;
    }

    if ($echo) {
        echo $output;
    }

    return $output;
}*/

/*function emm_paginate_loop($start, $max, $page = 0) {
    $output = "";
    for ($i = $start; $i <= $max; $i++) {
        $output .= ($page === intval($i))
            ? "<span class='emm-page emm-current'>$i</span>"
            : "<a href='" . get_pagenum_link($i) . "' class='emm-page'>$i</a>";
    }
    return $output;
}

function post_is_in_descendant_category( $cats, $_post = null )
{
    foreach ( (array) $cats as $cat ) {
        // get_term_children() accepts integer ID only
        $descendants = get_term_children( (int) $cat, 'category');
        if ( $descendants && in_category( $descendants, $_post ) )
            return true;
    }
    return false;
}*/

/*******************************
CUSTOM COMMENTS
 ********************************/

function mytheme_comment($comment, $args, $depth) {
    $GLOBALS['comment'] = $comment; ?>
   <li <?php comment_class('clearfix'); ?> id="li-comment-<?php comment_ID() ?>">
   	<div class="gravatar">
           <?php echo get_avatar($comment,$size='50',$default=get_bloginfo('template_directory').'/images/default_ava.png' ); ?>
           <div class="gravatar_mask"></div>
       </div>
<div id="comment-<?php comment_ID(); ?>">
    <div class="text">
        <?php comment_text() ?>
    </div>
    <div class="comment-meta commentmetadata clearfix">
        <?php printf(__('<font style="font-weight:bold;font-size:12px">%s</font>'), get_comment_author_link()) ?><?php /*edit_comment_link(__('(Edit)'),'  ','')*/ ?> <span><?php echo date('d-m-Y',strtotime(get_comment_date())).' '.date('H:i',strtotime(get_comment_time())); ?>
	  </span>
    </div>



    <?php if ($comment->comment_approved == '0') : ?>
    <em><?php _e('Your comment is awaiting moderation.') ?></em>
    <br />
    <?php endif; ?>

    <!--  <div class="reply">
         <?php comment_reply_link(array_merge( $args, array('depth' => $depth, 'max_depth' => $args['max_depth']))) ?>
      </div>-->
</div>
    <?php }

/*******************************
THEME OPTIONS PAGE
 ********************************/
/*
add_action('admin_menu', 'boldy_theme_page');
function boldy_theme_page ()
{
    if ( count($_POST) > 0 && isset($_POST['boldy_settings']) )
    {
        $options = array ('logo_img', 'logo_alt','contact_email','contact_text','cufon','linkedin_link','twitter_user','latest_tweet','facebook_link','keywords','description','analytics','copyright','home_box1','home_box1_link','home_box2','home_box2_link','home_box3','home_box3_link','blurb_enable','blurb_text','blurb_link','blurb_page', 'footer_actions','actions_hide','portfolio','blog','slider');

        foreach ( $options as $opt )
        {
            delete_option ( 'boldy_'.$opt, $_POST[$opt] );
            add_option ( 'boldy_'.$opt, $_POST[$opt] );
        }

    }
    add_menu_page(__('LBM Theme Config'), __('LBM Theme'), 'edit_themes', basename(__FILE__), 'boldy_settings');
    add_submenu_page(__('LBM Options'), __('LBM Options'), 'edit_themes', basename(__FILE__), 'boldy_settings');
}*/
/*******************************
CONTACT FORM
 ********************************/

#
# Group menu footer - custom by JKey
#


/* FORMAT NUMBER - BY JKEY */
/*function format_number($num, $dec_place)
{
    $number = number_format($num,$dec_place,',','.');
    return $number;
}*/
/* END FORMAT NUMBER - BY JKEY */
/*
	CUSTOM BY JKEY
*/


/*
require(TEMPLATEPATH . '/includes/custom_taxonomy.php' );
require(TEMPLATEPATH . '/includes/meta_box.php');
require(TEMPLATEPATH . '/includes/custom_fields.php');
require(TEMPLATEPATH . '/includes/custom_permalinks.php');
require(TEMPLATEPATH . '/includes/custom_widget.php');*/

?>